import router from "@/router";

const state = {
  routea: null,
};

const mutations = {
  SET_BREADCRUMB: (state, routea) => {
    state.routea = routea;
  },
};
let routea = [];
function routeFillter(route, routes) {
  for (let i = 0; i < route.length; i++) {
    // console.log(route)
    // console.log(routes)
    routes.map((item) => {
      if (!item.children) {
        if (item.path == route[i].path) {
          if (item.meta && item.meta.title) {
            // console.log(item.meta.title,item.path)
            routea.push({
              title: item.meta.title,
              path: item.path,
            });
          }
        }
      } else {
        if (
          item.path == route[i].path ||
          (item.path == "/" && route[i].path == "")
        ) {
          if (item.meta && item.path !== "/" && item.meta.title) {
            // console.log(item.meta.title)
            routea.push({
              title: item.meta.title,
              path: item.path,
            });
          }
          routeFillter(route, item.children);
        }
      }
    });
  }
  // return name;
}

const actions = {
  Breadcrumb({ commit }, route) {
    let routes = router.options.routes;
    routea = [];
    routeFillter(route, routes);
    // console.log(name.indexOf('首页'))
    if (routea[0] && routea[0].title !== "首页") {
      routea.unshift({
        title: "首页",
        path: "/home",
      });
    }
    // console.log(routea)
    commit("SET_BREADCRUMB", routea);
  },

  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      console.log(userInfo);
      if (userInfo.username == "admin" && userInfo.password == "123456") {
        resolve(true);
      } else {
        resolve(false);
      }
      reject("还未对接口");
      commit();
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
